﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class PhilosopherBemaService : BaseService, IPhilosopherBema
    {

        public List<PhilosopherBema> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.PhilosopherBema.OrderBy(c=>c.Order).ThenByDescending(c => c.UpdateTime).ToList();
            }
        }

        public List<PhilosopherBema> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.PhilosopherBema.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title == searchparma.Ttile);
                }
                if (searchparma.ClassType >= 0)
                {
                    query = query.Where(c => c.ClassType == searchparma.ClassType);
                }
                if (searchparma.LectcherID > 0)
                {
                    query = query.Where(c => c.LecturerID.Split(',').Contains(searchparma.LectcherID.ToString()));
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime <= searchparma.EndDateTime);
                }
                query = base.Paging<PhilosopherBema, DateTime?>(c => c.Order,c=>c.UpdateTime, query, searchparma.Pagination);
                return query.ToList();
            }
        }

        public List<VBemaLecturerInfo> GetAllAndLecturer(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.VBemaLecturerInfo.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title.IndexOf(searchparma.Ttile) >= 0 || c.LecturerName.IndexOf(searchparma.Ttile) >= 0);
                }
                if (!string.IsNullOrEmpty(searchparma.Lectcher))
                {
                    query = query.Where(c => c.LecturerName.IndexOf(searchparma.Lectcher)>=0);
                }
                if (searchparma.ClassType >= 0)
                {
                    query = query.Where(c => c.ClassType == searchparma.ClassType);
                }
               
                if (searchparma.LectcherID > 0)
                {
                    string lectcherID = Convert.ToString(searchparma.LectcherID) + ",";
                    query = query.Where(c => (c.LecturerID + ",").IndexOf(lectcherID) > 0);
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime <= searchparma.EndDateTime);
                }
                query = base.Paging<VBemaLecturerInfo, DateTime?>(c => c.Order, c => c.UpdateTime, query, searchparma.Pagination);

                return query.ToList();
            }
        }


        public PhilosopherBema GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.PhilosopherBema.Where(c => c.ID == id).FirstOrDefault();
            }
        }

        public PhilosopherBema GetByClassType(int classType)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.PhilosopherBema.Where(c => c.ClassType == classType).FirstOrDefault();
            }
        }

        public VBemaLecturerInfo GetAndLecturerByClassType(int classType)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.VBemaLecturerInfo.Where(c => c.ClassType == classType).FirstOrDefault();
            }
        }

        public VBemaLecturerInfo GetAndLecturerByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.VBemaLecturerInfo.Where(c => c.ID == id).FirstOrDefault();
            }
        }

        public VBemaLecturerInfo GetForIndex()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.VBemaLecturerInfo.Where(c=>c.ClassType==2).OrderByDescending(c => c.IsIndex).ThenByDescending(c => c.UpdateTime).FirstOrDefault();
            }
        }

        public List<VBemaLecturerInfo> GetForTop(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.VBemaLecturerInfo.Where(c => c.ClassType == searchparma.ClassType);
                query = base.Paging<VBemaLecturerInfo, DateTime?>(c => c.Order, c => c.UpdateTime, query, searchparma.Pagination);
                return query.ToList();
            }
        }
        public bool Add(PhilosopherBema model)
        {
            try
            {
                this.Add<PhilosopherBema>(model);
                UpdateBemaLecturer(model);
                return true;
            }
            catch { return false; }
        }

        private void UpdateBemaLecturer(PhilosopherBema model)
        {
            this.Delete<BemaLecturerRelation>(c => c.BemaID == model.ID);
            if (!string.IsNullOrEmpty(model.LecturerID))
            {
                int lecturer = 0;
                foreach (var item in model.LecturerID.Split(','))
                {
                    if (int.TryParse(item, out lecturer))
                    {
                        var blModel = new BemaLecturerRelation() { BemaID = model.ID, Lecturer = lecturer };
                        this.Add<BemaLecturerRelation>(blModel);
                    }

                }
            }
        }

        public bool Update(PhilosopherBema model)
        {
            try
            {
                this.Update<PhilosopherBema>(model);
                UpdateBemaLecturer(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int id)
        {
            try
            {
                this.Delete<PhilosopherBema>(c => c.ID == id);
                return true;
            }
            catch { return false; }
        }


    }
}
